/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
/*****************************************************************************
 * Name:tiny_event_router.c
 *
 * Purpose: event router implementation
 *     
 * Version: 0.1
 *
 * Developer:
 *     wen.gu,2017-03-09
 *
 *
 *****************************************************************************/
#include <string.h>

#include "general_macro.h"
#include "vos_mem.h"
#include "vos_mutex.h"
#include "vos_condition.h"

#include "tiny_event_router_internal.h"
#include "tiny_event_looper_internal.h"
#include "tiny_event_looper.h"




/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
#define LOGE printf
#define LOGD printf
#define LOGW printf
/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/
/***************************************************************************
 *
 * inner API define 
 *
 ***************************************************************************/
static GS32 tlThreadLooper(void* param)
{
	my_looper_t* ml = (my_looper_t*)param;
	tiny_list_t* event_queue = &(ml->event_queue);
	my_event_t* me = NULL;
	tiny_event_t* evt = NULL;
	void* opaque = ml->opaque;
	tfLooperOnEventFunc_t on_event = ml->on_event;
	GPHD lock = ml->lock;
	GPHD queue_changed_cond = ml->queue_changed_cond;

	while (ml->is_request_eixt == GFALSE)
	{

		vosMutexLock(lock);
		me = (my_event_t*)tfListFront(event_queue);

		if (me == NULL)
		{
			vosCondWait(queue_changed_cond, lock);
			continue;
		}
		else
		{
			evt = me->evt;
			tfListPopFront(event_queue);
		}

		vosMutexUnlock(lock);

		on_event(opaque, evt);

		/** todo, */
		tfEventDestroy(evt);
	}

	return 0; /** todo, refine return value */
}

static my_event_t* telEventCreate(tiny_event_t* tvt)
{
	my_event_t* me = (my_event_t*)memAlloc(sizeof(my_event_t));
	GCHECK(me);
	return me;
}

static void telEventClears(my_looper_t* ml)
{
	tiny_list_t* event_queue = &(ml->event_queue);
	tiny_list_node_t* node = tfListFront(event_queue);

	while (node)
	{
		my_event_t* me = (my_event_t*)node;

		tfEventDestroy(me->evt);
		tfListPopFront(event_queue);
		node = tfListFront(event_queue);
	}

}


void telPost(my_looper_t* ml, tiny_event_t* evt)
{
	my_event_t* me = NULL;
	vosMutexLock(ml->lock);
	me = telEventCreate(evt);

	tfListPushBack(&(ml->event_queue), (tiny_list_node_t*)me);

	vosMutexUnlock(ml->lock);
}
/***************************************************************************
 *
 *  API define 
 *
 ***************************************************************************/
GPHD tfLooperCreate(void* opaque, tfLooperOnEventFunc_t on_evt_cb)
{
	my_looper_t* ml = NULL;

	if (opaque && on_evt_cb)
	{
		ml = (my_looper_t*)memAlloc(sizeof(my_looper_t));

		if (ml)
		{
			memset(ml, 0, sizeof(*ml));
			tfListInitialize(&(ml->event_queue), memFree);
			ml->lock = vosMutexCreate();

			GCHECK(ml->lock);

			ml->queue_changed_cond = vosCondCreate();
			GCHECK(ml->queue_changed_cond);

			ml->opaque = opaque;
			ml->on_event = on_evt_cb;
			ml->tid = VTHD_INVALID_TID;

			//todo something
		}
		else
		{
			LOGE("alloc looper instance failed\n");
		}
	}
	else
	{
		LOGE("bad parameter\n");
	}

	return ml;
}


void tfLooperDestroy(GPHD looper)
{
	if (looper)
	{
		my_looper_t* ml = (my_looper_t*)looper;

		if (ml->tid != VTHD_INVALID_TID)
		{
			vosMutexLock(ml->lock);
			ml->is_request_eixt = GTRUE;
			vosCondBroadcast(ml->queue_changed_cond);
			vosMutexUnlock(ml->lock);

			vosThreadJoin(ml->tid);
		}

		if (ml->lock)
		{
			vosMutexFree(ml->lock);
		}

		if (ml->queue_changed_cond)
		{
			vosCondFree(ml->queue_changed_cond);
		}

		telEventClears(ml);

		terUnregisterLooper(ml->router, ml->id);

		memFree(looper);
	}
}


GS32 tfLooperStart(GPHD looper, const char* name, GS32 priority)
{
	GS32 ret = G_ErrorBadParameter;

	if (looper)
	{
		my_looper_t* ml = (my_looper_t*)looper;

		vosMutexLock(ml->lock);
		if (ml->tid == VTHD_INVALID_TID)
		{
			vos_thread_id tid;
			vos_thread_attr_t attr;
			memset(&attr, 0, sizeof(attr));
			attr.name = name;
			attr.priority = priority;
			attr.joinable = GTRUE;
			ml->is_request_eixt = GFALSE; /** when destroy need set this value to GTRUE */

			ret = vosThreadCreate(&tid, tlThreadLooper, ml, &attr);

			if (G_OK == ret)
			{
				ml->tid = tid;
			}
		}
		else
		{
			LOGE("looper already startting\n");
			ret = G_ErrorInvalidOperation;
		}

		vosMutexUnlock(ml->lock);
	}

	return ret;
}


void tfLooperPost(GPHD looper, tiny_event_t* evt)
{
	if (looper && evt)
	{
		my_looper_t* ml = (my_looper_t*)looper;

		vosMutexLock(ml->lock);

		if (ml->router)
		{
			terPost(ml->router, evt);
		}
		else
		{
			LOGE("looper havn't register to router, so we cann't post event\n");
		}

		vosMutexUnlock(ml->lock);
	}
}


void tfLooperReply(GPHD looper, tiny_event_t* evt, GU32 reply_id)
{
	if (looper && evt)
	{
		my_looper_t* ml = (my_looper_t*)looper;

		vosMutexLock(ml->lock);

		if (ml->router)
		{
			terReply(ml->router, evt, reply_id);
		}
		else
		{
			LOGE("looper havn't register to router, so we cann't reply\n");
		}

		vosMutexUnlock(ml->lock);
	}
}


GS32 tfLooperPostAndWaitResponse(GPHD looper, tiny_event_t* evt, tiny_event_t** response)
{
	GS32 ret = G_ErrorBadParameter;
	if (looper && evt)
	{
		my_looper_t* ml = (my_looper_t*)looper;

		vosMutexLock(ml->lock);

		if (ml->router)
		{
			ret = terPostAndWaitResponse(ml->router, evt, response);
		}
		else
		{
			ret = G_ErrorInvalidOperation;
			LOGE("looper havn't register to router, so we cann't post\n");
		}

		vosMutexUnlock(ml->lock);
	}

	return ret;
}